package com.ruoyi.controller.admin.accounting;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.admin.AdminXiaoYuanReimbursement;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.poi.MyExcelUtils;
import com.ruoyi.system.service.admin.AdminXiaoYuanReimbursementService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.LinkedHashMap;
import java.util.List;

@RestController
@RequestMapping("/accounting/xiaoyuan")
public class AdminXiaoYuanReimbursementController extends BaseController {

    @Resource
    private AdminXiaoYuanReimbursementService adminXiaoYuanReimbursementService;

    /**
     * 查询后台-小院报销列表
     */
    @PreAuthorize("@ss.hasPermi('accounting:xiaoyuan:list')")
    @GetMapping("/list")
    public TableDataInfo list(AdminXiaoYuanReimbursement adminXiaoYuanReimbursement) {
        return adminXiaoYuanReimbursementService.selectAdminXiaoYuanReimbursementList(adminXiaoYuanReimbursement);
    }

    /**
     * 导出后台-小院报销列表
     */
    @PreAuthorize("@ss.hasPermi('accounting:xiaoyuan:export')")
    @Log(title = "后台-小院报销", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AdminXiaoYuanReimbursement adminXiaoYuanReimbursement) {
        // 查询出数据
        List<AdminXiaoYuanReimbursement> dataList = (List<AdminXiaoYuanReimbursement>)adminXiaoYuanReimbursementService.selectAdminXiaoYuanReimbursementList(adminXiaoYuanReimbursement).getRows();
        List<LinkedHashMap<String, Object>> linkedHashMaps = MyExcelUtils.convertToMapList(dataList);
        // 特殊处理字段 documentVoucher
        ExcelUtil<AdminXiaoYuanReimbursement> util = new ExcelUtil<AdminXiaoYuanReimbursement>(AdminXiaoYuanReimbursement.class);
        util.exportExcel(response, dataList, "后台-记账数据");
//        MyExcelUtils.generateExcel(response,linkedHashMaps , "后台-小院报销数据", Collections.singletonList("documentVoucher"));
    }

    /**
     * 获取后台-小院报销详细信息
     */
    @PreAuthorize("@ss.hasPermi('accounting:xiaoyuan:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(adminXiaoYuanReimbursementService.selectAdminXiaoYuanReimbursementById(id));
    }

    /**
     * 新增后台-小院报销
     */
    @PreAuthorize("@ss.hasPermi('accounting:xiaoyuan:add')")
    @Log(title = "后台-小院报销", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AdminXiaoYuanReimbursement adminXiaoYuanReimbursement) {
        return toAjax(adminXiaoYuanReimbursementService.insertAdminXiaoYuanReimbursement(adminXiaoYuanReimbursement));
    }

    /**
     * 修改后台-小院报销
     */
    @PreAuthorize("@ss.hasPermi('accounting:xiaoyuan:edit')")
    @Log(title = "后台-小院报销", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AdminXiaoYuanReimbursement adminXiaoYuanReimbursement) {
        return toAjax(adminXiaoYuanReimbursementService.updateAdminXiaoYuanReimbursement(adminXiaoYuanReimbursement));
    }

    @PreAuthorize("@ss.hasPermi('accounting:xiaoyuan:list')")
    @GetMapping("/getTime")
    public AjaxResult getTime() {
        return AjaxResult.success(adminXiaoYuanReimbursementService.getTime());
    }


    // timeSave
    @PreAuthorize("@ss.hasPermi('accounting:xiaoyuan:add')")
    @Log(title = "后台-小院报销", businessType = BusinessType.INSERT)
    @PostMapping("/timeSave")
    public AjaxResult timeSave(@RequestBody AdminXiaoYuanReimbursement adminXiaoYuanReimbursement) {
        adminXiaoYuanReimbursementService.timeSave(adminXiaoYuanReimbursement);
        return success();
    }

    @PreAuthorize("@ss.hasPermi('accounting:xiaoyuan:edit')")
    @Log(title = "后台-小院报销", businessType = BusinessType.UPDATE)
    @GetMapping("/clearTime")
    public AjaxResult clearTime() {
        adminXiaoYuanReimbursementService.timeRemove();
        return success();
    }

//    /**
//     * 删除后台-小院报销
//     */
//    @PreAuthorize("@ss.hasPermi('accounting:xiaoyuan:remove')")
//    @Log(title = "后台-小院报销", businessType = BusinessType.DELETE)
//    @DeleteMapping("/{ids}")
//    public AjaxResult remove(@PathVariable Long[] ids) {
//        return toAjax(adminXiaoYuanReimbursementService.deleteAdminXiaoYuanReimbursementByIds(ids));
//    }
}
